package com.jjb.www.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jjb.www.R;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.PreListApi;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.activity.CopyActivity;
import com.jjb.www.ui.activity.PlanDetitleActivity;
import com.jjb.www.ui.activity.PreEmploymentEducationActivity;
import com.jjb.www.ui.adapter.PreAdapter;
import com.jjb.www.widget.CommonListRefreshView;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 可进行拷贝的副本
 */
public final class PreListFragment extends AppFragment<CopyActivity> implements BaseAdapter.OnItemClickListener {
    private static final String INTENT_KEY_TRAIN_TYPE = "train_type";
    private static final String INTENT_KEY_SENTRY_ID = "sentry_id";
    private static final String INTENT_KEY_REGION_ID = "region_id";
    private PreAdapter mAdapter;
    private CommonListRefreshView mRefreshView;


    public static PreListFragment newInstance(String train_type, String sentry_id, String region_id) {
        PreListFragment fragment = new PreListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_TRAIN_TYPE, train_type);
        bundle.putString(INTENT_KEY_SENTRY_ID, sentry_id);
        bundle.putString(INTENT_KEY_REGION_ID, region_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pre;
    }

    @Override
    protected void initView() {
        mAdapter = new PreAdapter(getContext());
        mRefreshView = findViewById(R.id.refreshView);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void initData() {
        mRefreshView.setDataHelper(new CommonListRefreshView.DataHelper<PreListApi.Bean>() {
            @Override
            public AppAdapter<PreListApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpList<PreListApi.Bean>> callback) {
                EasyHttp.get(getActivity())
                        .api(new PreListApi()
                                .setRegion_id(getString(INTENT_KEY_REGION_ID))
                                .setSentry_id(getString(INTENT_KEY_SENTRY_ID))
                                .setTrain_type(getString(INTENT_KEY_TRAIN_TYPE))
                                .setPage(p)
                                .setPageSzie(mAdapter.getSize())
                        ).request(new HttpCallback<HttpList<PreListApi.Bean>>(getAttachActivity()) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpList<PreListApi.Bean> result) {
                        callback.onSucceed(result);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<PreListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<PreListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("plan_type","2");
        intent.putExtra("className",mAdapter.getItem(position).getClass_time());
        intent.putExtra("id",mAdapter.getItem(position).getId()+"");
        intent.setClass(getContext(), PreEmploymentEducationActivity.class);
        startActivity(intent);
    }
}